//
//  StudyTabBarController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "StudyTabBarController.h"
#import "NeedStudyViewController.h"
#import "ResourceViewController.h"
#import "OptionalStudyViewController.h"
#import "RankingViewController.h"
#import "ZJTabBarBurron.h"
#import "ZJNavigationController.h"
#import "StudyCoureVideoViewController.h"
#import "StudyGetLastVideoModel.h"
#import "ZJStudySpacialDetailModel.h"
#import <UIView+WZLBadge.h>
#define BUTNTG 1200
@interface StudyTabBarController ()<ZJTabBarBurronDelegate>
{
    NSArray *_imageName;/**< 未选中的图片组 */
    NSArray *_selectImageName;/**< 选中的图片组 */
    NSInteger _lastIndex;
    ZJTabBarBurron *_playerButton;
}

@end

@implementation StudyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.tintColor = [UIColor whiteColor];
    [self configs];
    [self netWorking_BXNumber];
    
}



- (void)configs{
    //1、初始化标签控制器管理的视图控制器
    ResourceViewController *resourceVc = [[ResourceViewController alloc]init];
    NeedStudyViewController *needVc = [[NeedStudyViewController alloc]init];
    UIViewController *addVc = [[UIViewController alloc]init];
    OptionalStudyViewController *opStudyVc = [[OptionalStudyViewController alloc]init];
    RankingViewController *rankingVc = [[RankingViewController alloc]init];
    //2、将视图控制器存入数组 便于以后统一配置
    NSArray *vcs = @[resourceVc,needVc,addVc,opStudyVc,rankingVc];
    
    //3、准备工作
    NSArray *vctitles = @[@"资源",@"必学",@"",@"选学",@"排行"];
    _imageName = @[@"mine_tab_ziyuan_d",@"mine_tab_bixue_d",@"mine_tab_play",@"mine_tab_xuanxue_d",@"mine_tab_paihang_d"];
    _selectImageName = @[@"mine_tab_ziyuan_h",@"mine_tab_bixue_h",@"mine_tab_stop",@"mine_tab_xuanxue_h",@"mine_tab_paihang_h"];
    NSMutableArray *navs = [@[] mutableCopy];
    //4、配置
    [vcs enumerateObjectsUsingBlock:^(UIViewController * _Nonnull vc, NSUInteger idx, BOOL * _Nonnull stop) {
        vc.title = vctitles[idx];
        //        vc.view.backgroundColor = [UIColor whiteColor];
        ZJNavigationController *nav = [[ZJNavigationController alloc]initWithRootViewController:vc];
        //设置导航栏的背景颜色
        nav.navigationBar.barTintColor = [UIColor whiteColor];
        //设置导航栏的标题属性（字体/字体颜色）NSForegroundColorAttributeName 表示前景色
        nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        nav.navigationBar.hidden = YES;
        
        [navs addObject:nav];
        
    }];
    
    //5、配置标签栏
    self.viewControllers = navs;
    self.tabBar.barTintColor = [UIColor whiteColor];
    //6、自定义标签栏
    //6.1、移除标签栏上面的所有子视图
    for (UIView *subview in self.tabBar.subviews) {
        //        if ([subview isKindOfClass:[UIButton class]]) {
        //            [subview removeFromSuperview];
        //        }
        
        [subview removeFromSuperview];
        
    }
    
    UIView *buttonBGView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - LP_TabBarHeight, self.view.width, LP_TabBarHeight)];
    buttonBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:buttonBGView];
    [self.view bringSubviewToFront:buttonBGView];
    
    //6.2、自定义按钮
    
    CGFloat btuW = [UIScreen mainScreen].bounds.size.width / vcs.count;
    CGFloat btuH = self.tabBar.bounds.size.height;
    
    for (int i = 0; i < vcs.count; i ++) {
        
        ZJTabBarBurron *button = [[ZJTabBarBurron alloc]initWithFrame:CGRectMake(i * btuW, self.view.height - LP_TabBarHeight, btuW, btuH)];
        
        [button setTitle:vctitles[i] forState:ZJTabBarBurronStateNormal];
        [button setTitleColor:[UIColor zj_colorWithHexString:@"a7a7a7"] forState:ZJTabBarBurronStateNormal];
        [button setTitleColor:[UIColor themeColor] forState:ZJTabBarBurronStateSelected];
        button.tag = BUTNTG + i;
        
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",_imageName[i]]] forState:ZJTabBarBurronStateNormal];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",_selectImageName[i]]] forState:ZJTabBarBurronStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        
        if (i == 2) {
            CGFloat but2Width = 68 * [UIScreen zj_screenWScale];
            button.imageView.frame = CGRectMake((btuW - but2Width) / 2, 49 - but2Width, but2Width, but2Width);
            _playerButton = button;
        }
        
        if (i == 0) {
            button.selected = YES;
            _lastIndex = button.tag;
        }else{
            button.selected = NO;
        }
        button.delegate = self;
        button.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:button];
        [self.view bringSubviewToFront:button];
    }
    
    //去掉tabBar顶部线条
    CGRect rect = CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight]);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, self.view.height - LP_TabBarHeight, 159 * [UIScreen zj_screenWScale], 0.5)];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen zj_screenWith] - label.width, self.view.height - LP_TabBarHeight, label.width, 0.5)];
    label.backgroundColor = [UIColor zj_colorWithHexString:@"dedede"];
    label1.backgroundColor = [UIColor zj_colorWithHexString:@"dedede"];
    [self.view addSubview:label];
    [self.view addSubview:label1];
}


- (void)action_customButtonClick:(ZJTabBarBurron *)sender{
    if (sender.tag ==  BUTNTG + 2) {//点击最近的一次比方视频
        if ([[NSUserDefaults standardUserDefaults] objectForKey:LastVideoDict]) {//本地有播放记录
            ZJStudySpacialDetailModel *study = [ZJStudySpacialDetailModel yy_modelWithJSON:[[NSUserDefaults standardUserDefaults] objectForKey:LastVideoDict]];
            
            StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
            vc.model = study;
            vc.isModel = YES;
            vc.block = ^(ZJStudySpacialDetailModel *model) {
                _playerButton.selected = NO;
            };
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
            
        }else{
            [self netWorkingVodel];
        }
        return;
    }
    
    ZJTabBarBurron *button = [self.view viewWithTag:_lastIndex];
    button.selected = NO;
    //  切换控制器
    self.selectedIndex = sender.tag - BUTNTG;

    sender.selected = YES;
    _lastIndex = sender.tag;
}
#pragma mark - 网络请求
//选中视频播放。 进行网络请求
- (void)netWorkingVodel{
    [NetWorkingManager postWithUrlString:[NetWorkingURL getVideo] Parameter:@{} Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            StudyGetLastVideoModel *model1 = [StudyGetLastVideoModel yy_modelWithJSON:object[@"ST_COURSEWARE_ONE"]];
            if (!model1) {
                [YJProgressHUD showMsgWithoutView:@"暂无最近播放的视频"];
            }else{
                ZJStudySpacialDetailModel *study = [ZJStudySpacialDetailModel new];
                study.NAME = model1.NAME;
                study.CLICKRATE = model1.CLICKRATE;
                study.ID = model1.ID;
                study.TEACHERVIDEO = model1.TEACHERVIDEO;
                study.IMAGEPHOTO = model1.IMAGEPHOTO;
                study.CREATETIME = model1.CREATETIME;
                study.COURSETYPE = model1.COURSETYPE;
                study.COMMENT = model1.COMMENT;
                study.TEACHERVIDEO = model1.TEACHERVIDEO;
                study.GRADE = model1.GRADE;
                study.DURATION = model1.DURATION;
                StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
                vc.model = study;
                vc.isModel = YES;
                vc.block = ^(ZJStudySpacialDetailModel *model) {
                    _playerButton.selected = NO;
                };
                [LPCostomPressentAnimationSimilarityPushAnimation enterNextVc];
                ZJNavigationController *nav = [[ZJNavigationController alloc] initWithRootViewController:vc];
                [self presentViewController:nav animated:NO completion:nil];
            
            }
            

        }
    } Fail:^(id object) {
        
    }];
}
//必学下标
- (void)netWorking_BXNumber{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID1":[ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID ?[ZJUserInfoModel sharedUserInfo].USER_MODEL_BOOK.ID :@"",
                           @"USER_ID2":[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.ID ? [ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.ID : @"",
                           @"PAGE_NUMBER":@"1",
                           @"SHOW_NUMBER":@"100"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL mustLearn] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [[NSArray yy_modelArrayWithClass:[ZJStudySpacialDetailModel class] json:object[@"MUSTSTUDY"]] mutableCopy];
            [weakSelf showBIXueNumber:array.count];
        }
    } Fail:^(id object) {
    }];
    
}

- (void)showBIXueNumber:(NSInteger)number{
    UIButton *button = [self.tabBar viewWithTag:BUTNTG  + 1];
    [button.imageView showBadgeWithStyle:WBadgeStyleNumber value:number animationType:WBadgeAnimTypeNone];
}

#pragma mark - 屏幕旋转
//// 哪些页面支持自动转屏
//- (BOOL)shouldAutorotate{
//    UIViewController *vc = self.viewControllers[self.selectedIndex];
//    if ([vc isMemberOfClass:[ZJNavigationController class]]) {
//        UIViewController *topVC = ((UINavigationController *)vc).topViewController;
//        if ([topVC isMemberOfClass:[StudyCoureVideoViewController class]]) {
//            return YES;
//        }
//    }
//    return NO;
//}
//
//// 支持哪些转屏方向
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//    UIViewController *vc = self.viewControllers[self.selectedIndex];
//    if ([vc isMemberOfClass:[ZJNavigationController class]]){
//        ZJNavigationController *nav = (ZJNavigationController *)vc;
//        if ([nav.topViewController isMemberOfClass:[StudyCoureVideoViewController class]]) {
//            return UIInterfaceOrientationMaskAllButUpsideDown;
//        }else { // 其他页面支持转屏方向
//            return UIInterfaceOrientationMaskPortrait;
//        }
//    }
//    return UIInterfaceOrientationMaskPortrait;
//}
//
@end
